﻿using Models;
using MySqlSugar;
using Repositories.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Utility;

namespace Repositories.Repository
{
    public class RolemenuRepository : IRolemenuRepository
    {
        protected readonly SqlSugarClient _db;
        public RolemenuRepository()
        {
            _db = SugarDao.GetInstance();
        }

        #region 查询

        /// <summary>
        /// 查询单条根据主键
        /// </summary>
        public RolemenuModel GetById(int? id)
        {
            var model = _db.Queryable<RolemenuModel>().InSingle(id);
            return model;
        }

        /// <summary>
        /// 查询所有
        /// </summary>
        public Queryable<RolemenuModel> GetList()
        {
            return _db.Queryable<RolemenuModel>();
        }

        /// <summary>
        /// 根据条件查询
        /// </summary>
        public Queryable<RolemenuModel> GetList(Expression<Func<RolemenuModel, bool>> where)
        {
            return _db.Queryable<RolemenuModel>().Where(where);
        }

        /// <summary>
        /// 按页查询 OrderFields string 支持复杂【id desc,name asc】
        /// </summary>
        public Queryable<RolemenuModel> GetPageList(Expression<Func<RolemenuModel, bool>> where, int pageIndex, int pageSize, string OrderFields)
        {
            switch (OrderFields)
            {
                default:
                    return _db.Queryable<RolemenuModel>().Where(where).OrderBy(it => it.Id, OrderByType.desc).Skip((pageIndex - 1) * pageSize).Take(pageSize);

            }
        }
#endregion

        #region 删除
        /// <summary>
        /// 根据roleid删除
        /// </summary>
        public bool DeleteById(int id)
        {
            return _db.Delete<ReplyModel, int>(id);
        }

        /// <summary>
        /// 根据主键组批量删除
        /// </summary>
        public bool DeleteById(int[] ids)
        {
            return _db.Delete<RolemenuModel, int>(ids);
        }
        
        /// <summary>
        /// 根据实体删除【一定要带主键！！！】
        /// </summary>
        public bool DeleteByModel(RolemenuModel model)
        {
            return _db.Delete(model);
        }

        #endregion

        #region 新增
        /// <summary>
        /// 强制新增，不管主键
        /// </summary>
        public int Insert(RolemenuModel model)
        {
            return int.Parse(_db.Insert(model).ToString());
        }

        /// <summary>
        /// 批量插入 适合海量数据插入
        /// </summary>
        public bool InsertRange(List<RolemenuModel> list)
        {
            return _db.SqlBulkCopy(list);
        }

        #endregion

        #region 修改
        /// <summary>
        /// 根据实体更新【主键一定要有！！！】
        /// </summary>
        public bool Update(RolemenuModel model)
        {
            return _db.Update(model);
        }

        /// <summary>
        /// 10条以上批量更新
        /// </summary>
        public bool UpdateRange(List<RolemenuModel> list)
        {
            return _db.SqlBulkReplace(list);
        }


        #endregion

        public TransactionResult UpdateAll(int RoleId,int Disable)
        {
            TransactionResult result = new TransactionResult();
            _db.CommandTimeOut = 30000;//设置超时时间
            try
            {
                _db.BeginTran();//开启事务
                //db.BeginTran(IsolationLevel.ReadCommitted);+3重载可以设置事世隔离级别
                List<RolemenuModel> list = _db.Queryable<RolemenuModel>().Where(i => i.Role_Id == RoleId).ToList();
                foreach (RolemenuModel m in list)
                {
                    _db.Delete(m);
                }
                List<MenuModel> list2 = _db.Queryable<MenuModel>().ToList();
                List<RolemenuModel> insertlist = new List<RolemenuModel>();
                foreach (MenuModel m in list2)
                {
                    RolemenuModel rmodel = new RolemenuModel()
                    {
                        Disable = Disable,
                        Menu_Id = m.Id,
                        Role_Id = RoleId
                    };
                    insertlist.Add(rmodel);
                }
                _db.SqlBulkCopy(insertlist);
                _db.CommitTran();//提交事务
                return result;
            }
            catch (Exception ex)
            {
                _db.RollbackTran();//回滚事务
                result.isError = false;
                result.message = ex.Message;
                return result;
            }
            
        }

    }
}

